<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form id="form" action="">
        <input id="getName" type="text">
        <input id="getAge" type="text">
        <input id="getSex" type="text">
        <button id="btn">提交</button>
    </form>
    <script>
        const form =document.querySelector('#form')
        const getName =document.querySelector('#getName')
        const getAge =document.querySelector('#getAge')
        const getSex =document.querySelector('#getSex')
        const btn =document.querySelector('#btn')
        form.onsubmit =function(){
            //收集表单数据
            const nameValue = getName.value
            const AgeValue = getAge.value
            const SexValue = getSex.value
            const xhr = new XMLHttpRequest()
            xhr.open('post','/adduser?a=111',true)
            //body如果是查询字符串格式，则书写请求头xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');,服务器需要使用中间件app.use(express.urlencoded())
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
            xhr.send(`name=${nameValue}&age=${AgeValue}&sex=${SexValue}`)

            //body如果是json字符串格式，则书写请求头xhr.setRequestHeader("content-type", "application/json"),服务器需要使用中间件app.use(express.json());
            // xhr.setRequestHeader("content-type", "application/json");
            // xhr.send(JSON.stringify({
            //     name:nameValue,
            //     age:AgeValue,
            //     sex:SexValue
            // }))
            xhr.onreadystatechange =function(){
                if(xhr.readyState===4){
                    console.log(xhr.responseText)
                }
            }

            return false
        }


    </script>
</body>
</html>